/**
 * Created by xz on 2017/6/22.
 */
var $tbody = $("#sample-table-1 tbody");

// 初始化table
$("#config_table").bootstrapTable({
    // 'url': '../serviceType/findAll',
    'url': '/community/forumconfiguration/getForumconfiguration',
    'toolbar': $('div[data-name="btnCtl"]'),
    // 'queryParams': queryParams,
    'cache':false,
    'sidePagination': 'server',
    'toolbarAlign': 'right',
    'undefinedText': '-',
    'striped': true,
    'pagination': true,
    'sortable': true, //是否启用排序
    'sortOrder': "asc", //排序方式
    'pageSize': globalPageSize,
    'pageList': [],
    'clickToSelect': true,
    'singleSelect': true,
    'smartDisplay': true,
    'ajaxOptions':{
        headers: {"Authorization": localStorage.token}
    },
    'responseHandler': function (resb) {
        return {
            'total': resb.data.length,
            'rows': resb.data
        }
    },
    'onCheck': function() {
        $("button[data-target='#updateModal'], button[data-target='#deleteModal']").removeAttr("disabled");
    },
    'onUncheck': function() {
        $("button[data-target='#updateModal'], button[data-target='#deleteModal']").attr("disabled", 'true');
    },
    'columns': [{
        'checkbox': true,
        // 'class': 'hide'
    },
        {
            'field': 'forumConfigurationId',
            'title': 'ID',
            'align': 'center',
            'class': 'hide'
        },{
            'field': 'nearbyMsgDistance',
            'title': '范围',
            'align': 'center'
        },{
            'field': 'heatMsgLikesPointCount',
            'title': '热度',
            'align': 'center'
        },{
            'field': 'heatMsgCommentCount',
            'title': '评论数',
            'align': 'center'
        },{
            'field': 'nearbyMsgDays',
            'title': '间隔时间',
            'align': 'center'
        },{
            'field': 'updateDate',
            'title': '更新日期',
            'align': 'center'
        }
    ]
});

/**
 *  配置修改ajax
 *  @param data
 */
function updata() {
  $.ajaxSetup({cache:false});
  return $.ajax({
      type: "POST",
      url: '/community/forumconfiguration/updataForumconfiguration',
      data: {
          'forumConfigurationId': $('#roUname').val(),
          'nearbyMsgDistance': $('#nearbyMsgDistance').val(),
          'heatMsgLikesPointCount': $('#heatMsgLikesPointCount').val(),
          'heatMsgCommentCount': $('#heatMsgCommentCount').val(),
          'nearbyMsgDays': $('#nearbyMsgDays').val()
      },
      dataType: 'json',
      beforeSend: function (request) {
          request.setRequestHeader("Authorization", localStorage.token);
      },
      success: function (resb) {
          alert(resb.message);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
          // console.log(XMLHttpRequest, textStatus, errorThrown);
          alert(textStatus);
      }
  })
};

$(document).on('click','.spanIcons',function(){
    $('#inputStation').val("");
    if($('#inputStation').val() ==""){
        $('.spanIcons').removeClass('glyphicon glyphicon-remove');
    }
});
$("#inputStation").keyup(function(){
    if($('#inputStation').val() !=""){
        $('.spanIcons').addClass('glyphicon glyphicon-remove');
    }
    if($('#inputStation').val() ==""){
        $('.spanIcons').removeClass('glyphicon glyphicon-remove');
    }
})

/**
 * 更新模态框显示
 */
$("#updateModal").on('show.bs.modal', function () {
     var tableData = $("#config_table").bootstrapTable("getSelections")[0];
    $('#roUname').val(tableData.forumConfigurationId);
    $('#nearbyMsgDistance').val(tableData.nearbyMsgDistance);
    $('#heatMsgLikesPointCount').val(tableData.heatMsgLikesPointCount);
    $('#heatMsgCommentCount').val(tableData.heatMsgCommentCount);
    $('#nearbyMsgDays').val(tableData.nearbyMsgDays);
});

$('.updateBtn').on('click',function () {
    var that = $(this);
    $('#updateModal').modal('hide');
    updata().done(function () {
        $("#config_table").bootstrapTable("refresh");
        $("button[data-target='#updateModal'], button[data-target='#deleteModal']").attr("disabled", 'true');
        that.parents('.modal-content').find('form')[0].reset();
    });
});
